//
//  HWGameCommunityDetailTopView.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/10/21.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameCommunityDetailTopView: UIView {
    var headImageView:UIImageView!
    var nameLabel:UILabel!
    var titleLabel:UILabel!
    var tagBgView:UIView!
    var tagLabel:UILabel!
    var timeLabel:UILabel!
    /// 置顶标签
    var isTopTag:UILabel!
    var isVideoTag:UILabel!
    var bestImageView:UIImageView!
    
    @objc var model:HWGameCommunityDetailModel? {
        didSet {
            headImageView.kf.setImage(with: URL(string: model?.face ?? ""))
            nameLabel.text = model?.fabu
            if (model?.biaoqian.count ?? 0) > 0 {
                tagBgView.isHidden = false
                tagLabel.text = model?.biaoqian
            } else {
                tagBgView.isHidden = true
            }
            
            timeLabel.text = model?.last_time
            titleLabel.text = model?.title
            bestImageView.kf.setImage(with: URL(string: model?.jiajing_img ?? ""))
            
            if model?.is_best == true {
                isTopTag.text = "置顶"
                isTopTag.backgroundColor = .colorWithHexString(hexString: "#FA8C08")
                isTopTag.isHidden = false
                isTopTag.snp.updateConstraints { make in
                    make.left.equalTo(18*widthScale)
                }
                if (model?.video.count ?? 0) > 0 {
                    ///为视频
                    isVideoTag.isHidden = false
                    isVideoTag.snp.makeConstraints { make in
                        make.left.equalTo(60*widthScale)
                    }
                    
                    titleLabel.snp.updateConstraints { make in
                        make.left.equalTo(102*widthScale)
                    }
                } else {
                    isVideoTag.isHidden = true
                    titleLabel.snp.updateConstraints { make in
                        make.left.equalTo(60*widthScale)
                    }
                }
            } else {
                if model?.is_jiajing == true {
                    isTopTag.text = "精华"
                    isTopTag.backgroundColor = .colorWithHexString(hexString: "#FF726A")
                    isTopTag.isHidden = false
                    isTopTag.snp.updateConstraints { make in
                        make.left.equalTo(18*widthScale)
                    }
                    if (model?.video.count ?? 0) > 0 {
                        ///为视频
                        isVideoTag.isHidden = false
                        isVideoTag.snp.makeConstraints { make in
                            make.left.equalTo(60*widthScale)
                        }
                        
                        titleLabel.snp.updateConstraints { make in
                            make.left.equalTo(102*widthScale)
                        }
                    } else {
                        isVideoTag.isHidden = true
                        titleLabel.snp.updateConstraints { make in
                            make.left.equalTo(60*widthScale)
                        }
                    }
                } else {
                    isTopTag.isHidden = true
                    if (model?.video.count ?? 0) > 0 {
                        isVideoTag.isHidden = false
                        isVideoTag.snp.makeConstraints { make in
                            make.left.equalTo(18*widthScale)
                        }
                        
                        titleLabel.snp.updateConstraints { make in
                            make.left.equalTo(60*widthScale)
                        }
                    } else {
                        isVideoTag.isHidden = true
                        titleLabel.snp.updateConstraints { make in
                            make.left.equalTo(18*widthScale)
                        }
                    }
                }
                
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension HWGameCommunityDetailTopView {
    private func initUI() -> Void {
        headImageView = UIImageView()
        headImageView.layer.masksToBounds = true
        headImageView.contentMode = .scaleAspectFill
        headImageView.layer.cornerRadius = 33/2*widthScale
        addSubview(headImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 13*widthScale)
        addSubview(nameLabel)
        
        tagBgView = UIView()
        tagBgView.layer.cornerRadius = 4*widthScale
        tagBgView.backgroundColor = .colorWithHexString(hexString: "#FFECEB")
        addSubview(tagBgView)
        
        tagLabel = HWLabel.createBoldLabel(title: "", color: "#FF726A", font: 9*widthScale)
        tagBgView.addSubview(tagLabel)
        
        timeLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        addSubview(timeLabel)
        
        isTopTag = HWLabel.createBoldLabel(title: "置顶", color: "#FFFFFF", font: 11*widthScale)
        isTopTag.layer.cornerRadius = 4*widthScale
        isTopTag.layer.masksToBounds = true
        isTopTag.textAlignment = .center
        isTopTag.backgroundColor = .colorWithHexString(hexString: "#FA8C08")
        addSubview(isTopTag)
        
        isVideoTag = HWLabel.createBoldLabel(title: "视频", color: "#FFFFFF", font: 11*widthScale)
        isVideoTag.layer.cornerRadius = 4*widthScale
        isVideoTag.layer.masksToBounds = true
        isVideoTag.textAlignment = .center
        isVideoTag.backgroundColor = .colorWithHexString(hexString: "#333333")
        addSubview(isVideoTag)
        
        titleLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 14*widthScale)
        addSubview(titleLabel)
        
        bestImageView = UIImageView()
        addSubview(bestImageView)
        
        isTopTag.isHidden = true
        isVideoTag.isHidden = true
    }
    
    private func initFrame() -> Void {
        
        isTopTag.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalTo(19*widthScale)
            make.height.equalTo(18*widthScale)
            make.width.equalTo(33*widthScale)
        }
        
        isVideoTag.snp.makeConstraints { make in
            make.left.equalTo(60*widthScale)
            make.top.equalTo(19*widthScale)
            make.height.equalTo(18*widthScale)
            make.width.equalTo(33*widthScale)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(60*widthScale)
            make.right.equalTo(-18*widthScale)
            make.height.equalTo(20*widthScale)
            make.centerY.equalTo(isTopTag)
        }
        
        headImageView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalTo(56*widthScale)
            make.width.height.equalTo(33*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(5*widthScale)
            make.top.equalTo(headImageView)
            make.height.equalTo(18*widthScale)
        }
        
        tagBgView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(7*widthScale)
            make.height.equalTo(16*widthScale)
            make.centerY.equalTo(nameLabel)
        }
        
        tagLabel.snp.makeConstraints { make in
            make.left.equalTo(5*widthScale)
            make.right.equalTo(-5*widthScale)
            make.top.equalTo(2*widthScale)
            make.bottom.equalTo(-2*widthScale)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(2*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        bestImageView.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.top.equalTo(56*widthScale)
            make.width.equalTo(60*widthScale)
            make.height.equalTo(47*widthScale)
        }
    }
}
